extends KinematicBody2D

var speed=96
var pre_input_val=Vector2(0,1)
var cd_time=0.3
onready var cd_timer=cd_time
var attack_val=10
var bullet_speed=200
export(PackedScene) var bullet_tscn:PackedScene
var _loss_control=false
var velocity:Vector2


export(Array,Resource) var level_up_info_array

func _ready():
	var _e= Global.connect("game_over_signal",self,"_game_over")
	set_player_level_num(Global.play_level_num)
	pass # Replace with function body.

func _game_over():
	set_physics_process(false)

var _player_level_num=0 setget set_player_level_num,get_player_level_num
func set_player_level_num(player_level_num):
	if player_level_num>level_up_info_array.size()-1: return
	Global.play_level_num=player_level_num
	_player_level_num=player_level_num
	cd_time=level_up_info_array[player_level_num].cd_time
	attack_val=level_up_info_array[player_level_num].attack_val
	bullet_speed=level_up_info_array[player_level_num].bullet_speed
	$AnimatedSprite.play("Idle"+str(_player_level_num))

func get_player_level_num():
	return _player_level_num

func _physics_process(_delta):
	var input_val_h = Input.get_action_strength("ui_right")-Input.get_action_strength("ui_left")
	var input_val_v = Input.get_action_strength("ui_down")-Input.get_action_strength("ui_up")	
	if input_val_h!=0: input_val_v=0
	var input_val=Vector2(input_val_h,input_val_v)
	
	if _loss_control:
		_slip(input_val)
	else:
		_move(input_val)
	
func _slip(input_val):
	if !_slippery:
		_loss_control=false
		return
	if input_val!=Vector2.ZERO:
		self.rotation=atan2(input_val.x,-input_val.y)
		$AnimatedSprite.play("Run"+str(_player_level_num))
	velocity=velocity.move_toward(Vector2.ZERO,speed*0.05)
	velocity=move_and_slide(velocity)
	if velocity.length()<0.1:_loss_control=false
	
func _move(input_val):
	if input_val!=Vector2.ZERO:
	#	这里当改变方向为90度时，要去对正位置
		if pre_input_val.dot(input_val)==0:
			if input_val.x!=0:
				self.global_position.y=round(self.global_position.y/16)*16
			else:
				self.global_position.x=round(self.global_position.x/16)*16
		pre_input_val=input_val
		self.rotation=atan2(input_val.x,-input_val.y)
		velocity=move_and_slide(input_val*speed,Vector2.ZERO,false,1,0,false)
		$AnimatedSprite.play("Run"+str(_player_level_num))
	else:
		if _slippery and velocity!=Vector2.ZERO:
			_loss_control=true
		$AnimatedSprite.stop()
		
func _process(delta):
	cd_timer+=delta
	if Input.is_action_pressed("Attack"):
		if cd_timer>cd_time:
			_fire()
			cd_timer=0
			
var _invincible = false
func set_invincible(time):
	_invincible=true
	$Shield.play("Run")
	$InvincibleTimer.start(time)
func _on_InvincibleTimer_timeout():
	_invincible=false
	$Shield.play("Empty")
	pass # Replace with function body.

var _slippery = false
var _ice_num=0 setget set_ice_num,get_ice_num
func set_ice_num(ice_num):
	_ice_num=ice_num
	if ice_num>0:
		_slippery=true
	else:
		_slippery=false
func get_ice_num():
	return _ice_num
	
func _fire():
	var bullet:Area2D= bullet_tscn.instance()
	get_tree().current_scene.add_child(bullet)
	bullet.position=$FirePos.global_position
	bullet.attack_val=attack_val
	bullet.speed=bullet_speed
	bullet.rotation=self.rotation

func take_damage(_attack_val,_bullet_dir):
	if _invincible: return
	Global.set_life_num(Global.get_life_num()-1)
	Global.play_level_num=0
	get_tree().current_scene.get_node("BornPlayer").start_born()
	queue_free()
